package Agency.models;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
import Agency.views.*;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author wangari
 */
public class DeleteTenantModel extends javax.swing.JFrame {
    
    Connection conn= null;
   public  DeleteTenantView myDeleteView ;
   private ActionListener actionlistener;
    
   /**
    * instantiates a new object of delete view
    */
   public DeleteTenantModel(){
    myDeleteView= new DeleteTenantView();
    //myDeleteView.setVisible(true);
   }
   /**
    * clearing all the field by setting their value to null
    */
   public void clearTextFields(){
      myDeleteView.getTheIdNo().setText(null);
      myDeleteView.getThePhoneNum().setText(null);
      myDeleteView.getThePlotNum().setText(null);
      myDeleteView.getTheHseId().setText(null);
      myDeleteView.getTheOccupation().setText(null);
      myDeleteView.getTheName().setText(null);   
   }
   /**
    * searching the tenant by use of the identification number
    */
   
   public void searchTenant(){
         //int  saleId=Integer.parseInt(mydelete.getProductId().getText());
       String    theTenantsIdNo = JOptionPane.showInputDialog ( "Enter the Id number of the Tenant you want to delete");
         try {
             
       Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/MusyiAgency?user=root&password=");
 System.out.println("connected to database");
        PreparedStatement p =conn.prepareStatement("SELECT * FROM TenantsInformation WHERE TenantsIdNo = "+theTenantsIdNo+";");


  java.sql.ResultSet r = p.executeQuery ();
  while(r.next()) {
       myDeleteView.getTheIdNo().setText(r.getString("TenantsIdNo"));
      myDeleteView.getThePhoneNum().setText(r.getString("TenantsPhoneNumber"));
      myDeleteView.getTheHseType().setSelectedItem(r.getString("HouseType"));
      myDeleteView.getThePlotNum().setText(r.getString("HousePlotNum"));
      myDeleteView.getTheHseId().setText(r.getString("HouseId"));
      myDeleteView.getTheTown().setSelectedItem(r.getString("Town"));
      myDeleteView.getTheGender().setSelectedItem(r.getString("Gender"));
      myDeleteView.getTheOccupation().setText(r.getString("Occupation"));
       myDeleteView.getTheName().setText(r.getString("TenantsName"));
    
  }
}
       catch (Exception e) {
  JOptionPane.showMessageDialog(null,e);
        }
    }
  /**
   * deleting the tenant from the database
   */ 
   public void deleteTenant(){
    String  tenantIdNo = myDeleteView.getTheIdNo().getText();
        try {
  Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/MusyiAgency?user=root&password=");
        PreparedStatement p =conn.prepareStatement("Delete FROM TenantsInformation" + " WHERE TenantsIdNo=?");
        p.setString(1,tenantIdNo);
        p.execute();
        JOptionPane.showMessageDialog(null,"Tenant Deleted Successfully");
        clearTextFields();
    } catch(Exception e){
        System.out.println("Error"+e.toString());
        return;        
    }
      
  }
   /**
    * Closing the delete view
    */
   public void closeView(){
       clearTextFields();
       myDeleteView.setVisible(false);
   }
   
   
}
